package com.itcast.googleplay.holder;

import java.util.ArrayList;

import com.itcast.googleplay.PicActivity;
import com.itcast.googleplay.R;
import com.itcast.googleplay.bean.AppInfo;
import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class AppPicHolder extends BaseHolder<AppInfo>{

	private LinearLayout llContainer;
	private DisplayImageOptions options;
	private AppInfo appInfo;

	@Override
	public void initData() {
		appInfo = getData();
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
		
		LayoutParams layoutParams = new LinearLayout.LayoutParams
			(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.leftMargin = UIUtils.dip2px(6);
		layoutParams.topMargin = UIUtils.dip2px(6);
		layoutParams.bottomMargin = UIUtils.dip2px(6);
		for (int i = 0; i < appInfo.screen.size(); i++) {
			//循环几次创建几张图片
			ImageView imageView = new ImageView(UIUtils.getContext());
			ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+appInfo.screen.get(i), imageView,options);
			llContainer.addView(imageView,layoutParams);
			
			imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(UIUtils.getContext(), PicActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putStringArrayListExtra("imgList", (ArrayList<String>) appInfo.screen);
					UIUtils.getContext().startActivity(intent);
				}
			});
		}
	}

	@Override
	public View initView() {
		View view = UIUtils.inflate(R.layout.layout_app_pic_holder);
		llContainer = (LinearLayout) view.findViewById(R.id.ll_container);
		return view;
	}

}
